package com.superyee.commonlib.http;


import com.google.gson.internal.$Gson$Types;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public abstract class HttpResponseListener<T> {

    Type mType;

    public HttpResponseListener() {
        mType = getSuperclassTypeParameter(getClass());
    }

    Type getSuperclassTypeParameter(Class<?> subclass) {
        Type superclass = subclass.getGenericSuperclass();
        if (superclass instanceof Class) {
            throw new RuntimeException("Missing type parameter.");
        }
        ParameterizedType parameterized = (ParameterizedType) superclass;
        return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()[0]);
    }

    /**
     * 描述：获取数据开始.
     */
    public abstract void onStart(int action, String url);


    /**
     * 完成后调用，失败，成功，调用.
     */
    public abstract void onFinish(int action);

    /**
     * 描述：失败，调用.
     */
    public abstract void onFailure(int resultCode, int action, String error, Throwable throwable);


    /**
     * 描述：无网络
     */
    public abstract void noNetwork(int action);
//    /**
//     * 描述：网络状态回调
//     *
//     * @param isConnection 当前网络是否连接
//     * @param type 当前连接的网络类型
//     */
//    public abstract void onNetworkChange(boolean isConnection,int type);

    /**
     * 进度.
     *
     * @param bytesWritten the bytes written
     * @param totalSize    the total size
     */
    public void onProgress(int bytesWritten, int totalSize) {
    }


    /**
     * 服务器返回成功
     *
     * @param action
     * @param result
     * @param obj
     */
    public abstract void onSuccess(int statusCode, int action, T result, Object obj);

}
